package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.FloatWindow;
import com.company.project.service.FloatWindowService;
import com.company.project.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by CodeGenerator on 2019/07/11.
*/
@RestController
@RequestMapping("/float/window")
public class FloatWindowController {
    @Resource
    private FloatWindowService floatWindowService;

    @Resource
    private FileUploadUtil fileUploadUtil;

    @PostMapping("/add")
    public Result add(@RequestParam String floatWindow, MultipartFile file) {
        FloatWindow fw = JSON.parseObject(floatWindow,FloatWindow.class);
        boolean bo = false;
        String s = null;
        if(file!=null){
            s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
        }
        fw.setUri(fw.getUri());
        fw.setImgUri(s);
        if(fw.getId()==null || ("").equals(fw.getId())){
            bo = floatWindowService.insertFW(fw);
        }else{
            bo = floatWindowService.updateFw(fw);
        }
        if(bo){
            return ResultGenerator.genSuccessResult("保存成功");
        }
        return ResultGenerator.getInternalServerErrot("保存错误");
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        floatWindowService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(FloatWindow floatWindow) {
        floatWindowService.update(floatWindow);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        FloatWindow floatWindow = floatWindowService.findById(id);
        return ResultGenerator.genSuccessResult(floatWindow);
    }

    @GetMapping("/list")
    public Result list() {
        List<FloatWindow> list = floatWindowService.findAll();
        return ResultGenerator.genSuccessResult(list);
    }
}
